#include "PerlinNoise.h"
#include <SDL2/SDL.h>
#include <time.h>

#define WIDTH 640
#define HEIGHT 480

int main()
{
	srand(time(NULL));

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "Initialize error: %s\n", SDL_GetError());
		exit(-1);
	}

	SDL_Window *window = SDL_CreateWindow("Perlin Noise 1d", 
			SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
			WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
	if (window == NULL) {
		fprintf(stderr, "Create window error: %s\n", SDL_GetError());
		SDL_Quit();
		exit(-1);
	}

	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	if (renderer == NULL) {
		fprintf(stderr, "Create renderer error: %s\n", SDL_GetError());
		SDL_DestroyWindow(window);
		SDL_Quit();
		exit(-1);
	}

	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);

	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

	initPermutation();

	int quit = 0;
	int scale = HEIGHT / 2;
	SDL_Event e;
	while (!quit) {
		while (SDL_PollEvent(&e)) {
			if (e.type == SDL_QUIT)
				quit = 1;
		}

		for (int x = 0; x < WIDTH; x ++) {
			int y = (int) perlinNoise1d((double) rand() / RAND_MAX) * scale;
			printf("%d\n", y);
			SDL_RenderDrawPoint(renderer, x, y);
		}

		SDL_Delay(16);
	}

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();

	exit(0);
}
